package com.gongj.infoconfigboot.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class Return implements RabbitTemplate.ReturnCallback {

    Logger logger = LoggerFactory.getLogger(Return.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * 需要给ConfirmCallback赋值 不然不会走回调方法，默认是null
     */
    @PostConstruct
    public void init(){
        rabbitTemplate.setReturnCallback(this);
    }

    //处理交换机发送消息到队列失败，则执行此方法。
    @Override
    public void returnedMessage(Message message,
                                int replyCode, String replyText,
                                String exchange, String routingKey) {
        logger.info("交换机发送消息到队列失败=====》");
        logger.info("message = {}",new String(message.getBody()));
        logger.info("replyCode = {}",replyCode);
        logger.info("replyText = {}",replyText);
        logger.info("exchange = {}",exchange);
        logger.info("routingKey = {}",routingKey);
    }
}
